<?php

/**
 * 自动生成 SysDictionaryService
 * 
 * Apps/Common/Service/SysDictionaryService.class.php
 * admin @ 2015-06-11
 */

namespace Common\Service;

class SysDictionaryService {

  private static $instance;

  var $is_del = array(
    0 => '否' ,
    1 => '是' ,
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new SysDictionaryService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'order' => 99 ,
      'text' => '',
      'tip' => '',
      'create_date' => date('Y-m-d H:i:s'),
      'update_date' => date('Y-m-d H:i:s'),
      'expand' => '',
      'allow_del' => '0',
    );
  }
  
  function get_by_id( $id ){
    $SysDictionary = M('SysDictionary');
    $data = $SysDictionary->find( $id ) ;
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'pid' => '' ,
      'count' => FALSE,
      'sort' => 'sort',
      'order' => 'ASC',
    );

    $config = extend($config, $default);

    $SysDictionary = M('SysDictionary');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }
    
    if (!empty($config['pid'])) {
      $where['pid'] = $config['pid'];
    } else {
      $where['pid'] = array('exp' , ' is null') ;
    }

    if ($config['count']) {
      return $SysDictionary->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      if ( $config['page'] > 0 ) {
        $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      } else {
        $limit = '';
      }
      $data = $SysDictionary
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $SysDictionary->_sql();
      return $data ? $data : array();
    }
  }

  function create($data) {
    $SysDictionary = M('SysDictionary');
    $ret = $SysDictionary->add($data);
    if ($ret) {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    } else {
      return ajax_arr('添加失败', FALSE);
    }
  }

  function update($id, $data) {
    $SysDictionary = M('SysDictionary');

    $ret = $SysDictionary->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $SysDictionary = M('SysDictionary');
    $ret = $SysDictionary->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
  
  function _to_key_val( $data ) {
    $arr = array();
    foreach ( $data as $row ) {
      $arr[$row['id']] = $row['text'] ;
    }
    
    return $arr ;
  }
  
  function _return_get( $is_key_val , $config ) {
    $data = $this->get_by_cond($config);
    if ( $is_key_val ) {
      return $this->_to_key_val($data);
    }
    return $data ;
  }

  function get_sys_type( $is_key_val = TRUE ) {
    $config = array('pid' => 1 ,'page' => 0 );
    return $this->_return_get($is_key_val, $config) ;
  }
  
  function get_sys_user_status( $is_key_val = TRUE ) {
    $config = array('pid' => 4 ,'page' => 0 );
    return $this->_return_get($is_key_val, $config) ;
  }
  
  function get_mer_user_status( $is_key_val = TRUE ) {
    $config = array('pid' => 8 ,'page' => 0 );
    return $this->_return_get($is_key_val, $config) ;
  }
  
  function get_merchant_type( $is_key_val = TRUE ) {
    $config = array('pid' => 17 ,'page' => 0 );
    return $this->_return_get($is_key_val, $config) ;
  }
  
  function get_merchant_status( $is_key_val = TRUE ) {
    $config = array('pid' => 18 ,'page' => 0 );
    return $this->_return_get($is_key_val, $config) ;
  }
  
  function get_goods_status( $is_key_val = TRUE ) {
    $config = array('pid' => 12 ,'page' => 0 );
    return $this->_return_get($is_key_val, $config) ;
  }
    
  function get_wx_type( $is_key_val = TRUE ) {
    $config = array('pid' => 29 ,'page' => 0 );
    return $this->_return_get($is_key_val, $config) ;
  }
}
